package com.wistech.search.solr.bench.expression;

import java.util.concurrent.CyclicBarrier;

import com.greenpineyu.fel.Expression;
import com.greenpineyu.fel.FelEngine;

public class FelBenchmarkThread extends Thread {
	protected Expression expression;
	protected int repeats;
	protected int countMax;
	protected CyclicBarrier barrier;
	protected int threadNum;
	private DefaultValue defaultValue;
	private FelEngine fel;

	public FelBenchmarkThread(FelEngine fel, DefaultValue defaultValue,
			int threadNum, Expression expression, CyclicBarrier barrier,
			int repeats, int countMax) {
		this.fel = fel;
		this.defaultValue = defaultValue;
		this.threadNum = threadNum;
		this.expression = expression;
		this.barrier = barrier;
		this.repeats = repeats;
		this.countMax = countMax;
	}

	@Override
	public void run() {
		try {
			barrier.await();
			long beginTime = System.nanoTime();
			for (int repeat = 0; repeat < repeats; repeat++) {
				for (int count = 0; count < countMax; count++) {
					expression.eval(defaultValue.getFelValues(fel));
				}
			}
			long endTime = System.nanoTime();
			System.out.println("线程" + threadNum + "执行完毕，耗时："
					+ (endTime - beginTime) + "纳秒");
			barrier.await();

		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
